import { query, sendReissueCoupon, sendCoupon } from '@/services/marketCoupon';

export default {
  namespace: 'marketCoupon',
  state: {
    data: {
      list: [],
      pagination: {},
    },
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(query, payload);
      const { data } = response;
      yield put({
        type: 'save',
        payload: data,
      });
    },
    *dispatchCoupon({ payload, callback }, { call }) {
      const resp = yield call(sendCoupon, payload);
      const { state, data = [] } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
    *reissueCoupon({ payload, callback }, { call }) {
      const resp = yield call(sendReissueCoupon, payload);
      const { state, data = [] } = resp;
      if (resp && state === 0 && callback) {
        callback(data);
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
